package com.ckd.flyingtrip.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.javabean.UserInfoAddressBean;

import java.util.List;

public class UserInfoAddressAdapter extends RecyclerView.Adapter<UserInfoAddressAdapter.MyHolder> {

    private Context context;
    private List<UserInfoAddressBean.DataBean> dataBeans;

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onModifyItemClick(View view, int position);

        void onDeleteItemClick(View view, int position);
    }

    public UserInfoAddressAdapter(Context context, List<UserInfoAddressBean.DataBean> dataBeans) {
        this.context = context;
        this.dataBeans = dataBeans;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_userinfo_address_recyclerview, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.name.setText(dataBeans.get(position).getName());
        holder.phone.setText(dataBeans.get(position).getPhone());
        holder.address.setText(dataBeans.get(position).getRegion() + dataBeans.get(position).getAddress());

        if (dataBeans.get(position).getAddressDefault().equals("1")) {
            holder.defaultText.setVisibility(View.VISIBLE);
        } else {
            holder.defaultText.setVisibility(View.INVISIBLE);
        }

        holder.modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onModifyItemClick(v, position);
            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onDeleteItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView name, phone, address, defaultText, modify, delete;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.item_userinfo_address_recyclerview_name);
            phone = itemView.findViewById(R.id.item_userinfo_address_recyclerview_phone);
            address = itemView.findViewById(R.id.item_userinfo_address_recyclerview_address);
            defaultText = itemView.findViewById(R.id.item_userinfo_address_recyclerview_default);
            modify = itemView.findViewById(R.id.item_userinfo_address_recyclerview_modify);
            delete = itemView.findViewById(R.id.item_userinfo_address_recyclerview_delete);
        }
    }
}
